home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Text Processing / Alpha 5.63 / Tcl / SystemCode / shell.tcl < prev    next >
Encoding:
Text File  |  1993-11-20  |  4.9 KB  |  234 lines  |  [TEXT/ALFA]

  1.  
  2. ################################################################################
  3. # Shell routines.
  4. ################################################################################
  5.  
  6.  
  7. proc setShellMode {} {
  8.     setTclMode
  9.     changeMode "Csh"
  10.     insertMenu "Tcl"
  11. }
  12.  
  13. proc initShell {} {
  14.     insertText "Welcome to Alpha's Tcl shell."
  15.     insertText -w [lindex [winNames] 0] [shellPrompt]
  16. }
  17.  
  18. # Return the prompt. We want the window name because some of the commands
  19. # we evaluate (such as 'edit') open a new window, and we want the insertion
  20. # to be done in the shell window.
  21. proc shellPrompt {} {
  22.     regexp "(\[^:\]*):$" [pwd] crDum crDir
  23.     return "\r$crDir> "
  24. }
  25.  
  26.  
  27. # Called at all carriage returns.
  28. proc carriageReturn {} {
  29.     global mode
  30.     global indentOnCR
  31.     set indentString ""
  32.     deleteText [getPos] [selEnd]
  33.     if {$indentOnCR} {
  34.         set pos [getPos]
  35.         set text [getText [lineStart $pos] $pos]
  36.         for {set i 0; set len [string length $text]} {$i <= $len} {incr i} {
  37.             set c [string index $text $i]
  38.             if {($c != "\t") && ($c != "\ ")} {
  39.                 set indentString [string range $text 0 [expr $i-1]]
  40.                 break
  41.             }
  42.         }
  43.     }
  44.     insertText "\r" $indentString
  45. }
  46.  
  47.  
  48. proc tclCarriageReturn {} {
  49.     global mode
  50.     global _text
  51.     global _returnText
  52.     set pos [getPos]
  53.     set ind [string first ">" [getText [lineStart $pos] $pos]]
  54.     if {$ind < 0} {
  55.         carriageReturn
  56.         return
  57.     }
  58.     set lStart [expr [lineStart $pos]+$ind+2]
  59.     endOfLine
  60.     set _text [getText $lStart [getPos]]
  61.     set fileName [lindex [winNames] 0]
  62.     if {[getPos] != [maxPos]} {
  63.         goto [maxPos]
  64.         insertText -w $fileName $_text
  65.     }
  66.     if {[string first "Toolserver" $fileName] != -1} {
  67.         if {![catch {dosc -n ToolServer -s $_text} _returnText]} {
  68.             insertText "\r" $_returnText
  69.         } else {
  70.             insertText "\r"
  71.         }
  72.         mpwPrompt
  73.     } else {
  74.         uplevel #0 {catch $_text _returnText}
  75.         if {[string length $_returnText]} {
  76.             insertText -w $fileName "\r" $_returnText [shellPrompt]
  77.         } else {
  78.             insertText -w $fileName [shellPrompt]
  79.         }
  80.     }
  81.     unset _text
  82.     unset _returnText
  83. }
  84. bind '\r' carriageReturn
  85. bind '\r' tclCarriageReturn "Csh"
  86. bind '\r' tclCarriageReturn "MPW"
  87.  
  88. proc startMPW {} {
  89.     global toolserverPath
  90.  
  91.     if {![string length [checkRunning ToolServer MPSX toolserverPath]]} return
  92.  
  93.     insertText "Welcome to Alpha's MPW shell (using ToolServer via AppleEvents)."
  94.     bind '\r' tclCarriageReturn "MPW"
  95.     carriageReturn
  96.     mpwPrompt
  97. }
  98. proc mpwPrompt {} {
  99.     insertText "mpw> "
  100. }
  101.  
  102. proc setMPWMode {} {
  103.     changeMode "MPW"
  104. }
  105.  
  106. #    tclCarriageReturn
  107.  
  108.  
  109.  
  110. #=============================================================================
  111. #    Shell Aliases
  112. #=============================================================================
  113. proc l {args} {
  114.     eval [concat "ls -F" $args]}
  115.  
  116. proc ll {args} {
  117.     eval [concat "ls -l" $args]}
  118.  
  119.  
  120. proc grep {pat args} {
  121.     set args [glob -nocomplain $args]
  122.     foreach file $args {
  123.         set id [open $file]
  124.         if {[regexp $pat [read $id]]} {
  125.             close $id
  126.             set id [open $file]
  127.             while {[gets $id string] != "-1"} {
  128.                 if {[regexp $pat $string] == 1} {
  129.                     insertText \r$file: $string
  130.                 }
  131.             }
  132.             close $id
  133.         } else {
  134.             close $id
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. proc wc {args} {
  141.     set totChars 0
  142.     set totLines 0
  143.     set totWords 0
  144.     set args [glob -nocomplain $args]
  145.     foreach file $args {
  146.         set id [open $file]
  147.         set chars [string length [set text [read $id]]]
  148.         set lines [llength [split $text "\n"]]
  149.         set words [llength [split $text]]
  150.         insertText [format "\r%8d%8d%8d    $file" $lines $words $chars]
  151.         set totChars [expr $totChars+$chars]
  152.         set totWords [expr $totWords+$words]
  153.         set totLines [expr $totLines+$lines]
  154.         close $id
  155.     }
  156.     if {[llength $args] > 1} {
  157.         insertText [format "\r%8d%8d%8d    total" $totLines $totWords $totChars]
  158.     }
  159. }
  160.  
  161. proc cp args {
  162.     if {[set len [llength $args]] < 2} {
  163.         error "usage: cp <file1> <file2>\r       cp <file1> .... <dir>"
  164.     }
  165.     set len [expr $len-1]
  166.     regexp {.*[^:]} [lindex $args $len] dir
  167.     set args [lreplace $args $len $len]
  168.     set files {}
  169.     foreach arg $args {
  170.         append files " " [glob $arg]
  171.     }
  172.     if {[llength $files] == 1} {
  173.         set f [lindex $files 0]
  174.         if {[file exists $dir]} {
  175.             copyFile $f $dir:[file tail $f]
  176.         } else {
  177.             copyFile $f $dir
  178.         }
  179.     } else {
  180.         foreach f $files {
  181.             if {[catch {copyFile $f $dir:[file tail $f]}]} {
  182.                 alertnote "Error copying '$f'"
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. proc mv args {
  189.     if {[set len [llength $args]] < 2} {
  190.         error "usage: mv <file1> <file2>\r       mv <file1> .... <dir>"
  191.     }
  192.     set len [expr $len-1]
  193.     regexp {.*[^:]} [lindex $args $len] dir
  194.     set args [lreplace $args $len $len]
  195.     set files {}
  196.     foreach arg $args {
  197.         append files " " [glob $arg]
  198.     }
  199.     if {[llength $files] == 1} {
  200.         set f [lindex $files 0]
  201.         if {[file exists $dir]} {
  202.             moveFile $f $dir:[file tail $f]
  203.         } else {
  204.             moveFile $f $dir
  205.         }
  206.     } else {
  207.         foreach f $files {
  208.             if {[catch {moveFile $f $dir:[file tail $f]}]} {
  209.                 alertnote "Error copying '$f'"
  210.             }
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. proc rm args {
  217.     set files {}
  218.     foreach arg $args {
  219.         append files " " [glob $arg]
  220.     }
  221.     foreach f $files {
  222.         removeFile $f
  223.     }
  224. }
  225.  
  226.  
  227. proc cd args {
  228.     if {[llength $args]} {
  229.         changeDir [string trim [eval list $args] "\{\}"]
  230.     } else {
  231.         changeDir
  232.     }
  233. }
  234.